﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;

namespace BinaryTimesCore
{
    public static class BinaryTimesInterfaceReceiver
    {
        static FileSystemWatcher fileSystemWatcher;
        static string statusMessagesPath;

        public delegate void StatusMessagesChangedEventHandler();

        public static event StatusMessagesChangedEventHandler StatusMessagesChanged;

        static void OnStatusMessagesChanged()
        {
            StatusMessagesChangedEventHandler temp = StatusMessagesChanged;
            if (temp != null)
            {
                temp();
            }
        }

        static BinaryTimesInterfaceReceiver()
        {
            StatusMessages = new Dictionary<string, StatusMessage>();

            statusMessagesPath = Path.Combine(Path.GetTempPath(), "BinaryTimesStatusMessages");

            if (!Directory.Exists(statusMessagesPath))
                Directory.CreateDirectory(statusMessagesPath);

            fileSystemWatcher = new FileSystemWatcher(statusMessagesPath);
            fileSystemWatcher.Created += fileSystemWatcher_Created;
            fileSystemWatcher.Changed += fileSystemWatcher_Changed;
            fileSystemWatcher.Deleted += fileSystemWatcher_Deleted;
            fileSystemWatcher.EnableRaisingEvents = true;
            fileSystemWatcher.IncludeSubdirectories = false;
        }

        static void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(new Action<string>(DeleteStatusMessage), e.Name);
        }

        static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(new Action<string, string>(UpdateStatusMessages), e.Name, e.FullPath);
        }

        static void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(new Action<string, string>(UpdateStatusMessages), e.Name, e.FullPath);
        }

        public static Dictionary<string, StatusMessage> StatusMessages { get; private set; }

        public static void Dispose()
        {
            try
            {
                fileSystemWatcher.EnableRaisingEvents = false;
                fileSystemWatcher.Dispose();

                Directory.Delete(statusMessagesPath, true);
            }
            catch
            {
            }
        }

        static void DeleteStatusMessage(string id)
        {
            try
            {
                id = id.Replace("!", "");

                if (StatusMessages.ContainsKey(id))
                {
                    StatusMessages.Remove(id);
                    OnStatusMessagesChanged();
                }
            }
            catch { }
        }

        static void UpdateStatusMessages(string id, string filePath)
        {
            try
            {
                bool isCritical = id.Contains("!");
                id = id.Replace("!", "");
                string text = File.ReadAllText(filePath);

                if (!string.IsNullOrWhiteSpace(text))
                {
                    if (!StatusMessages.ContainsKey(id))
                    {
                        StatusMessages.Add(id, new StatusMessage
                        {
                            ID = id,
                            IsCritical = isCritical,
                            Text = text
                        });
                    }
                    else
                    {
                        StatusMessages[id].Text = text;
                        StatusMessages[id].IsCritical = isCritical;
                    }
                    OnStatusMessagesChanged();
                }
            }
            catch { }
        }
    }
}
